ifneq ($(KERNELRELEASE),)
obj-m := ipt_domain.o

else

KERNEL_SRC = ../linux-2.6.28
ifeq ($(KERNEL_SRC),)
$(error You need to define KERNEL_SRC)
endif

ifneq ($wildcard $(KERNEL_SRC)/include/linux/modversions.h),)
MODVERSIONS = -DMODVERSIONS
endif

_KVER = $(strip $(shell cat $(KERNEL_SRC)/Makefile | grep -e '^VERSION' | cut -d"=" -f2))
_KPL = $(strip $(shell cat $(KERNEL_SRC)/Makefile | grep -e '^PATCHLEVEL' | cut -d"=" -f2))
_KSUB = $(strip $(shell cat $(KERNEL_SRC)/Makefile | grep -e '^SUBLEVEL' | cut -d"=" -f2))
KERNEL_SERIES=$(_KVER).$(_KPL)

ifeq ($(KERNEL_SERIES), 2.6)
	TARGET=ipt_domain.ko
else
	TARGET=ipt_domain.o
endif

SED = sed
IPTABLES_BIN = iptables

ifndef $(IPTABLES_SRC)
IPTVER ?= \
	$(shell $(IPTABLES_BIN) --version | $(SED) -e 's/^iptables v//')
IPTABLES_SRC = ../../iptables/iptables-1.4.4
endif

ifeq ($(IPTABLES_SRC),)
IPTABLES_SRC = ../../iptables/iptables-1.4.4
endif

IPTABLES_INCLUDE = -I$(IPTABLES_SRC)/include
IPTABLES_VERSION = $(shell cat $(IPTABLES_SRC)/Makefile | grep -e '^IPTABLES_VERSION:=' | cut -d"=" -f2)
IPTABLES_OPTION = -DIPTABLES_VERSION=\"$(IPTABLES_VERSION)\"

CC = gcc
CFLAGS := -O2 -Wall 

all: modules libipt_domain.so

modules: $(TARGET)

ipt_domain.o: ipt_domain.h ipt_domain.c
	$(CC) $(CFLAGS) -I$(KERNEL_SRC)/include -c ipt_domain.c -D__KERNEL__ -DMODULE $(MODVERSIONS)

ipt_domain.ko: ipt_domain.h ipt_domain.c
	$(MAKE) -C $(KERNEL_SRC) SUBDIRS=$(PWD) modules


libipt_domain.so: libipt_domain.c ipt_domain.h
	$(CC) $(CFLAGS) $(IPTABLES_OPTION) $(IPTABLES_INCLUDE) -fPIC -c libipt_domain.c
	ld -shared -o libipt_domain.so libipt_domain.o

clean:
	-rm -f *.o *.so *.ko .*.cmd *.mod.c modules.order  Module.symvers

install: all
	cp -rf libipt_domain.so /lib/iptables/
	cp -rf $(TARGET) /lib/modules/`uname -r`/kernel/net/ipv4/netfilter/
	depmod -a
endif
